﻿using DailyReport.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RuoVea.ExDto;
using RuoVea.ExEnum;
using RuoVea.ExSugar;
using RuoVea.ExUtil;
using SqlSugar;

namespace DailyReport.Controllers
{
    [Authorize(Roles = "None")]
    public class WeeklyController : Controller
    {
        /// <summary>
        /// 
        /// </summary>
        private readonly SugarRepository<ReportDetail> _thisRepository;
        private readonly ICurrentUser _currentUser;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="thisRepository"></param>
        public WeeklyController(SugarRepository<ReportDetail> thisRepository, ICurrentUser currentUser)
        {
            _thisRepository = thisRepository;
            _currentUser = currentUser;
        }

        public IActionResult Index()
        {
            return View();
        }
        public IActionResult Timeline()
        {
            return View();
        }

        /// <summary>
        /// 查询所有/分页
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost("weekly/pages")]
        public async Task<RestfulResult<PageResult<ReportDetailOutDto>>> GetPagesAsync([FromBody] PageParam data)
        {
            var restful = new RestfulResult<PageResult<ReportDetailOutDto>> { Code = CodeStatus.OK };
 

            var query = await _thisRepository.AsQueryable()
                .Where(u => u.Creator == _currentUser.UserId)
                .Where(u => u.Category == Category.Week) 
                .OrderByDescending(u => u.Day)
                .Select(u => new ReportDetailOutDto
                {
                    Id = u.Id,
                    Day = u.Day,
                    Content = u.Content,
                    PlanContent=u.PlanContent,
                    Week=u.Week,
                })
                .ToPageAsync(data.PageNo, data.PageSize);

            restful.Data = query;
            return restful;
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost("weekly/submit")]
        public async Task<RestfulResult> SubmitAsync([FromBody] ReportDetail data)
        {
            RestfulResult restful = new() { Code = CodeStatus.BadRequest };

            if (data.Week == null) {
                restful.Message = "周不能空";
                return restful;
            }
            if (data.Content.IsNullOrWhiteSpace())
            {
                restful.Message = "周报内容不能空";
                return restful;
            }
            bool result = false;
            if (data.Id == 0)
            {
                data.Category = Category.Week;
                data.IsDelete = IsDelete.N;
                data.Month = data.Day.Month;
                result = await _thisRepository.InsertAsync(data);
            }
            else
            {
                result = await _thisRepository.UpdateAsync(data);
            }

            if (result)
            {
                restful.Code = CodeStatus.OK;
                restful.Data = result;
                return restful;
            }
            else
            {
                restful.Data = result;
                return restful;
            }
        }
    }
}
